<?php

/**
 * Ajax_ReattributeEvent.phtml - Réattribution d'un évènement
 *
 * @package
 * @author Alexis Eric
 * @copyright Copyright (c) 2012 - Erra Prod
 * @version $1.0$
 * @access public
 */



if (isset($_POST['RESIZE_SLOT']) && is_array($_POST['RESIZE_SLOT'])) {
    foreach ($_POST['RESIZE_SLOT'] as $eventData) {
        reattribute_event($eventData);
    }
}
if (isset($_POST['MOVE_EVENT']) && is_array($_POST['MOVE_EVENT'])) {
    foreach ($_POST['MOVE_EVENT'] as $calEvent) {
        reattribute_event($calEvent);
    }
}
if (isset($_POST['event_id']) && ctype_alnum($_POST['event_id'])) {
    reattribute_event($_POST);
}

/**
 * reattribute_event() - Réattribution d'un évènement
 * 
 * @param type $calEvent
 */
function reattribute_event($calEvent) { 
    if (isset($calEvent['id']) && ctype_alnum($calEvent['id'])) {

        $event_id = $calEvent['id'];
        $originSlots = Erra_Core_DatabasePdo::SELECT('SELECT slot_day, slot_start, slot_end FROM msa_slotobject WHERE msa_slotobject.event_id ="' . $event_id . '"');
        if (Erra_Core_DatabasePdo::rowCount()) {
            $originSlots->setFetchMode(PDO::FETCH_OBJ);
            $originSlots = $originSlots->fetch();
        }
        $db_data = array('last_sync' => time(), 'slot_start' => $calEvent['slot_start'], 'slot_end' => $calEvent['slot_end'], 'event_id' => $calEvent['id'], 'agenda_id' => $calEvent['agenda_id']);
        if ((isset($calEvent['occurency']) && $calEvent['occurency'] == 'original') || $calEvent['event_type'] == 'presta') {
            $db_data['slot_day'] = $calEvent['slot_day'];
        }
        $column_names = array_keys($db_data);
        $column_values = ajax_sanitize_input_for_db_insert(array_values($db_data));

        foreach ($column_names as $i => $column_name) {
            $request[] = $column_name . ' = ' . $column_values[$i];
        }
        // TODO reconsiderate
        $request = 'UPDATE msa_slotobject SET ' . implode(',', $request) . ' WHERE event_id="' . $event_id . '"';

        $result = Erra_Core_DatabasePdo::EXECUTE($request);
        Tools_GCalendarObject::deleteEventFromUniqid($event_id);
        Tools_GCalendarObject::addEventFromUniqId($event_id);
        // Client side
        //////////////

        $rdv = Erra_Core_DatabasePdo::SELECT('SELECT from_internet, msa_agendaobject.email AS presta_email, msa_agendaobject.telephone_portable AS presta_portable, slot_day, slot_start, nom, prenom, msa_clientobject.email AS user_mail, msa_clientobject.telephone_portable AS user_portable FROM msa_slotobject, msa_clientobject, msa_agendaobject WHERE msa_slotobject.event_id ="' . $event_id . '" AND msa_clientobject.id=msa_slotobject.client_id AND msa_agendaobject.id=msa_slotobject.agenda_id');
        if (is_object($rdv)) {
            // Edit rendez vous
            $rdv = $rdv->fetch();
            // Get placeholders
            $nom_complet = $rdv->nom . ' ' . $rdv->prenom;
            $nom_etablissement = Config_GeneralConfigObject::get('nom_etablissement');
            $nouvelle_date_reservation = date('d/m/Y', $rdv->slot_day);
            $nouvelle_heure_reservation = time_to_str_hour($rdv->slot_start);
            $placeHolders = array('{nom_complet}' => $nom_complet,
                '{nom_etablissement}' => $nom_etablissement,
                '{nouvelle_date_reservation}' => $nouvelle_date_reservation,
                '{nouvelle_heure_reservation}' => $nouvelle_heure_reservation);

            // On envoie des mails/sms seulement si le rendez-vous est dans la futur
            ////////////////////////////////////////////////////////////////////////
            if ($rdv->slot_day + $rdv->slot_start > time()) {
                $result = Erra_Utils_Tools::sendTemplatedMail($rdv->user_mail, 'Mail/Mail_RdvEditClient.phtml', $placeHolders);
                if (Config_GeneralConfigObject::get('sms_client_moverdv') == 1) {
                    $result = Erra_Utils_Tools::sendTemplatedSms($rdv->user_portable, 'Sms/Sms_RdvEditClient.phtml', $placeHolders);
                }
                if (Config_GeneralConfigObject::get('mail_team_moverdv') == 1) {
                    $result = Erra_Utils_Tools::sendTemplatedMail($rdv->presta_email, 'Mail/Mail_RdvEditPresta.phtml', $placeHolders);
                }
                // Don't send if 'from internet' is enabled and from_internet == 0
                if (Config_GeneralConfigObject::get('sms_team_moverdv') == 1 && !(Config_GeneralConfigObject::get('sms_team_internetonly') && !$rdv->from_internet)) {
                    $result = Erra_Utils_Tools::sendTemplatedSms($rdv->presta_portable, 'Sms/Sms_RdvEditPresta.phtml', $placeHolders);
                }
            }
        }
    }
}